package com.fingard.dsp.bank.directbank.baofoo01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IDeContract;
import com.fingard.dsp.bank.directbank.baofoo01.util.FormatUtil;
import com.fingard.dsp.bank.directbank.baofoo01.util.RsaCodingUtil;
import com.fingard.dsp.bank.directbank.baofoo01.util.SecurityUtil;
import com.fingard.dsp.bank.reqrespobj.contract.ContractState;
import com.fingard.dsp.bank.reqrespobj.decontract.ReqDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.RetAtsDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.detail.RetAtsDeContractDetail;
import com.fingard.io.FileHelper;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class BaoFoo01DeContract extends BaoFoo01Base implements IDeContract {

    @Override
    public RetAtsDeContract deContract(ReqDeContract p_req) throws Exception {
        RetAtsDeContract retAts = new RetAtsDeContract(p_req.reqHeader);
        RetAtsDeContractDetail tmpRetDetail = new RetAtsDeContractDetail();
        tmpRetDetail.reqSeqID = p_req.contractDetail.askNumber;
        try{
            ActSetItem actItem = getBankConfig().getActSetItem(p_req.contractDetail.recAct, "contract");
            if (actItem == null || !FileHelper.existFile(actItem.ownKeyStorePath)|| !FileHelper.existFile(actItem.oppCertFilePath)) {
                if (actItem == null) {
                    throw new Exception("未找到账号" + p_req.contractDetail.recAct + "对应的商户配置信息");
                } else {
                    throw new Exception("账号" + p_req.contractDetail.recAct + "对应的证书文件不存在");
                }
            }
            Map<String,String> postParams = new TreeMap<String,String>();
            postParams.put("send_time", Format.DateTimeFormat.format(new Date()));
            postParams.put("msg_id", p_req.contractDetail.askNumber);
            postParams.put("version", "4.0.0.0");
            postParams.put("terminal_id", actItem.termId);
            postParams.put("txn_type", "04");
            postParams.put("member_id", actItem.merchantId);
            String dgtl_envlp = "01|"+actItem.saltKey;
            dgtl_envlp = RsaCodingUtil.encryptByPubCerFile(SecurityUtil.Base64Encode(dgtl_envlp), actItem.oppCertFilePath);//公钥加密
            postParams.put("dgtl_envlp", dgtl_envlp);
            String ProtocolNo= p_req.contractDetail.authCode;
            WriteBankLogLn("加密前的ProtocolNo=" + ProtocolNo);
            ProtocolNo = SecurityUtil.AesEncrypt(SecurityUtil.Base64Encode(ProtocolNo), actItem.saltKey);//先BASE64后进行AES加密
            postParams.put("protocol_no", ProtocolNo);
            postParams.put("signature", sign(postParams,actItem));
            String[] tmpStrRet = sendToBank("contract",postParams,actItem);

            if (tmpStrRet[0].length() == 0) {
                Map<String, String> ReturnData = FormatUtil.getParm(tmpStrRet[1]);
                String resp_code = ReturnData.get("resp_code");
                if("S".equals(resp_code)){
                    String biz_resp_code = ReturnData.get("biz_resp_code");
                    tmpRetDetail.respCode = "0";
                    tmpRetDetail.respInfo = ReturnData.get("biz_resp_msg");
                    if("0000".equals(biz_resp_code)){
                        tmpRetDetail.deSignState = ContractState.SUCCESS;
                    }else {
                        tmpRetDetail.deSignState = ContractState.FAIL;
                    }
                }else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo =ReturnData.get("biz_resp_msg");
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsDeContract qryDeContract(ReqDeContract p_reqQry) throws Exception {
        return null;
    }
}
